package com.hencoder.hencoderpracticedraw1.practice;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.hencoder.hencoderpracticedraw1.MiscUtil;

import java.util.List;

/**
 * Created by zhanghs on 2017/7/14.
 */

public class Practice13BezierWaterWaveView2 extends View{
    private final String Tag = Practice13BezierWaterWaveView2.class.getSimpleName();
    private Context mContext;
    private int mMaxValue = 100;
    private int View_Default_Size = 300;
    private int mDefaultSize;
    /*完整的波浪长度*/
    private int mViewWidth;
    /*完整的波浪高度*/
    private int mViewHeight;
    /*水位线*/
    private int mLevelHeight;
    /*波长*/
    private int mWaveWidth= 400;
    /*波高*/
    private int mWaveHeight = 200;
    /**
     * 被隐藏的最左边的波形
     */
    private float mLeftSide;
    /**
     * 移动的距离
     */
    private float mMoveLen;
    /**
     * 水波平移速度
     */
    public static final float SPEED = 5f;
    /**
     * 二阶贝塞尔曲线
     */
    private List<Point> mPointsList;

    private Paint mWavePaint;
    private Paint linePaint;
    /**
     * 进度文字画笔
     */
    private Paint mTextPaint;

    /**
     * 边界画笔
     */
    private Paint mPaintCir;

    /**
     * 波纹路径
     */
    private Path mWavePath;
    private Path mWaveStrokePath;
    /*圆弧宽度*/
    private float mArcWidth = 20;
    private Point mCenterPoint;
    private RectF mRectF;
    private float mRadius;
    private float mPrecent;
    private float mCurrVaule;
    private Paint mBgPaintCir;

    public Practice13BezierWaterWaveView2(Context context) {
        this(context,null);
    }

    public Practice13BezierWaterWaveView2(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Practice13BezierWaterWaveView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mCenterPoint = new Point();
        mRectF = new RectF();
        mDefaultSize = MiscUtil.dipToPx(context, View_Default_Size);
        init();
        setProgress(35);
    }

    private void init() {
        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.RED);
        linePaint.setStrokeWidth(5);

        mWavePaint = new Paint();
        mWavePaint.setAntiAlias(true);
        mWavePaint.setColor(Color.BLUE);
        mWavePaint.setStyle(Paint.Style.FILL_AND_STROKE);

        mTextPaint = new Paint();
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(50);
        mTextPaint.setColor(Color.BLACK);

        mPaintCir = new Paint();
        mPaintCir.setAntiAlias(true);
        mPaintCir.setColor(Color.GREEN);
        mPaintCir.setStrokeWidth(20);
        mPaintCir.setStyle(Paint.Style.STROKE);

        mBgPaintCir = new Paint();
        mBgPaintCir.setAntiAlias(true);
        mBgPaintCir.setColor(Color.GRAY);
        mBgPaintCir.setStrokeWidth(20);
        mBgPaintCir.setStyle(Paint.Style.STROKE);

        mWavePath = new Path();
        mWaveStrokePath = new Path();

        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MiscUtil.measure(widthMeasureSpec,mDefaultSize),MiscUtil.measure(heightMeasureSpec,mDefaultSize));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        /*确定中心点*/
        mCenterPoint.x = w / 2;
        mCenterPoint.y = h / 2;
        /*确定高宽最小长度*/
        int minSize = (int) Math.min((w - getPaddingLeft() - getPaddingRight() - mArcWidth * 2), (h - getPaddingTop() - getPaddingBottom() - mArcWidth * 2));
        /*确定圆的半径*/
        mRadius = minSize / 2;
        /*确定内切圆边界*/
        mRectF.left = mCenterPoint.x - mRadius - mArcWidth / 2;
        mRectF.top = mCenterPoint.y - mRadius - mArcWidth / 2;
        mRectF.right = mCenterPoint.x + mRadius + mArcWidth / 2;
        mRectF.bottom = mCenterPoint.y + mRadius + mArcWidth / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.CYAN);
        drawLine(canvas);
        drawClicle(canvas);
        drawProgress(canvas);
    }


    private void drawProgress(Canvas canvas) {
        canvas.drawText(String.format("%.0f", mCurrVaule) + "%", mCenterPoint.x, mCenterPoint.y + MiscUtil.measureTextHeight(mTextPaint) / 2, mTextPaint);
    }

    private void drawLine(Canvas canvas) {
        /*绘制基准新*/
        canvas.drawLine(0, mCenterPoint.y, mCenterPoint.x*2, mCenterPoint.y, linePaint);
        canvas.drawLine(mCenterPoint.x, 0, mCenterPoint.x, mCenterPoint.y * 2, linePaint);
    }

    private void drawClicle(Canvas canvas) {
        float currentAngle = 360 * mPrecent;
        Log.d(Tag, "mPrecent=" + mPrecent + "  currentAngle=" + currentAngle);

        /*绘制圆背景*/
        canvas.drawArc(mRectF, currentAngle, 360 - currentAngle, false, mBgPaintCir);
        /*绘制进度*/
        canvas.drawArc(mRectF, 0, currentAngle, false, mPaintCir);
    }

    public void setProgress(float value) {
        if(value < 0){
            throw new IllegalArgumentException("progress not less than 0");
        }
        if(value > mMaxValue){
            value = mMaxValue;
        }
        /*初始化圆环*/
        initCircle(value);
    }

    private void initCircle(float value) {
        float start = mPrecent;
        float end = value / mMaxValue;
        Log.d(Tag, "start=" + start + "  end=" + end);
        startAnimation(start,end);
    }

    private void startAnimation(float start, float end) {
        ValueAnimator anim = ValueAnimator.ofFloat(start, end);
        anim.setDuration(1800L);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPrecent = (float) animation.getAnimatedValue();
                mCurrVaule = mPrecent * mMaxValue;
                invalidate();
            }
        });
        anim.start();
    }

    private void initWaveAnimation() {

    }
}
